{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2023-06-26T14:04:42.323257800Z",
     "start_time": "2023-06-26T14:04:42.300268900Z"
    }
   },
   "outputs": [],
   "source": [
    "from pathlib import Path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "data": {
      "text/plain": "'身是菩提树，心如明镜台，时时勤拂拭，莫使有尘埃。\\n菩提本非树，明镜亦非台，本来无一物，何处染尘埃。\\n'"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 读取文件内容\n",
    "p1 = Path('../Test/这是测试.txt')\n",
    "# with p1.open(encoding='utf8') as f:\n",
    "#     print(f.read())\n",
    "p1.read_text(encoding='utf8')\n",
    "p1.read_bytes()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-06-26T14:06:52.623113400Z",
     "start_time": "2023-06-26T14:06:52.580122500Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [],
   "source": [
    "# 写入\n",
    "p2 = Path('temp.txt')\n",
    "# with p2.open('w', encoding='utf8') as f:\n",
    "#     f.write('满纸荒唐言')\n",
    "p2.write_text('满纸荒唐言',encoding='utf8')\n",
    "# p2.write_bytes(b'abc')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-06-26T14:07:31.434279800Z",
     "start_time": "2023-06-26T14:07:31.410283Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "data": {
      "text/plain": "(2, 3)"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(\"我\".encode('gbk')), len(\"我\".encode('utf8'))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-06-24T04:29:31.008010600Z",
     "start_time": "2023-06-24T04:29:30.898236900Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "身是菩\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "with p1.open(encoding='utf8') as f:\n",
    "    print(f.tell())\n",
    "    print(f.read(3))\n",
    "    print(f.tell())"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-06-24T05:45:07.410193600Z",
     "start_time": "2023-06-24T05:45:07.349411300Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "data": {
      "text/plain": "1"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len('\\n'.encode('utf8'))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-06-24T04:42:13.351120800Z",
     "start_time": "2023-06-24T04:42:13.302595700Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-06-24T05:45:26.939304100Z",
     "start_time": "2023-06-24T05:45:26.912279100Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
